package com.snowflakes.gruntr;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.parse.ParseUser;

/*Notes here:
 * Check lines 184/185 of ParseSignupFragment.java to see where friend count and post count were added to user
 * More fields could be added here as well. Could even consider changing from friend count and
 *  just making an object of friends that attaches to each user. Similar to what is described below.
 *  It would be like a linked list of friends growing out of the user. Once we figure out relational queries and/or
 *  associations (see below by a few lines) we may be able to cross check with that to know who can see what posts and all.
 *  Also, I'd like to see if it's possible to get the ObjectID value, because if so, that's our hashing done for us.
 *  We store that in the "linked list" of friends and simply use those to find the other friend's things.
 *  It's also interesting to note that Parse automatically stores the date that something is created, so we can hopefully work
 *  that into a database query and sort by most recent, thus allowing us to have the feed sorted chronologically.
 * 
 * Important code about associations
 * 
 * Associations involving a ParseUser work right of the box. For example, let's say you're making a blogging app. 
 * To store a new post for a user and retrieve all their posts:

ParseUser user = ParseUser.getCurrentUser();
 
// Make a new post
ParseObject post = new ParseObject("Post");
post.put("title", "My New Post");
post.put("body", "This is some great content.");
post.put("user", user);
post.saveInBackground();
 
// Find all posts by the current user
ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
query.whereEqualTo("user", user);
query.findInBackground(new FindCallback<ParseObject>() { ... });

//
//

So the idea is simpler than it sounds.
        Each post is an object tied to the user
        Just as I saved a file into the object before, you could save a file into that object as well
        And it is theoretically even possible to save the comments into that post as well
        Although we'd need to work out authentication for it, since only the user who creates data has permission to modify it by default.
        Simplest idea is to check if they are friends, and give them permission to modify data for the duration of the comment creation.
        Realize that the data is anything associated with the user, since users can have their own data.
        So when I'm talking about permission to modify data, that would include adding a comment to a post that the user made, since
        that post would be his or her data.
 */


public class SplashScreen extends Activity {
        private static final int LOGIN_REQUEST = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        
        ActionBar actionBar = getActionBar();
        actionBar.hide();
        
        //Remove if else block in the finally block and just execute the code in else if you want to log in every time
        //Useful for tweaking log in screen details (like background or text color)
        //Also useful for making multiple users
        
        Thread timer = new Thread() {
                        public void run(){
                                try{
                                        sleep(2000);
                                } catch(InterruptedException e){
                                        e.printStackTrace();
                                } finally{
                                        ParseUser currentUser = ParseUser.getCurrentUser();
                                        //If user is logged in go to app
                                        if(currentUser != null) {
                                                Intent i = new Intent(SplashScreen.this, GruntrMain.class);
                                        i.putExtra("User",  ParseUser.getCurrentUser().getString("name"));
                                        startActivity(i);
                                        finish();
                                        }
                                        //else show log in
                                        else {
                                                ParseLoginBuilder builder = new ParseLoginBuilder(SplashScreen.this);
                                        Intent parseLoginIntent = builder.setAppLogo(R.drawable.orangebanner)
                                            .setParseLoginEnabled(true)
                                            .setParseLoginButtonText("Go")
                                            .setParseSignupButtonText("Register")
                                            .setParseLoginHelpText("Forgot password?")
                                            .setParseLoginInvalidCredentialsToastText("Your email and/or password is not correct")
                                            .setParseLoginEmailAsUsername(true)
                                            .setParseSignupSubmitButtonText("Submit registration")
                                            .build();
                                        startActivityForResult(parseLoginIntent, LOGIN_REQUEST);
                                        }                               
                                
                               
                                }
                        }//public void run()
                }; //new Thread()
                timer.start();
        
        
    }

    @Override
    protected void onActivityResult(int requestCode, int ResultCode, Intent data){
         if (requestCode == LOGIN_REQUEST) {
                        if(ResultCode == RESULT_OK) {
                                Intent i = new Intent(SplashScreen.this, GruntrMain.class);
                                i.putExtra("User",  ParseUser.getCurrentUser().getString("name"));
                                startActivity(i);
                                finish();
                        }
                        else {
                                Intent i = new Intent(SplashScreen.this, Goodbye.class);
                                startActivity(i);
                                finish();
                        }
                }
    }
    
        @Override
        protected void onPause() {
                // TODO Auto-generated method stub
                super.onPause();
        }
        
        
    
    
}